package com.bjkcst.baselib.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: glc.
 * Date: 2022/6/20  9:54
 * Description:
 */
public class TimeUtils {

    public static SimpleDateFormat YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

    public static SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

    public static SimpleDateFormat YYYY_MM_DD_HH_MM_SS_ = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.ENGLISH);

    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static int getSecondTimestampTwo(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Integer.parseInt(timestamp);
    }

    /**
     * <获取当天0点时间戳>
     *
     * @param time 到毫秒的时间戳
     * @return 当天0点时间戳
     * @throws
     */
    public static Long getToday0Time(long time) {
        Long oneDayTimestamps = Long.valueOf(60 * 60 * 24 * 1000);
        return time - (time + 60 * 60 * 8 * 1000) % oneDayTimestamps;
    }

    /**
     * <获取当天23:59:59时间戳>
     *
     * @param time 到毫秒的时间戳
     * @return 当天23:59:59时间戳
     * @throws
     */
    public static Long getToday23Time(long time) {
        Long oneDayTimestamps = Long.valueOf(60 * 60 * 24 * 1000);
        return time - (time + 60 * 60 * 8 * 1000) % oneDayTimestamps + 24 * 60 * 60 * 1000 -
                1000;
    }

    /**
     * 例：转化为 为yyyy-MM-dd HH:mm:ss 格式  dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     * ============================时间戳转换===========================
     *
     * @param createTime 时间戳 10位（字符串）
     * @return
     */
    public static String getSimpleDateTime(String createTime, SimpleDateFormat dateFormat) {
        //将服务器换回的String 时间戳转换成long类型数据
        if (null == createTime || createTime.isEmpty()) {
            return "";
        }
        if (createTime.length() != 10) {
            return createTime;
        }
        try {
            long time = Long.parseLong(createTime);
            //发布时毫秒
            long issueTime = new Date(time * 1000).getTime();
            //例 yyyy-MM-dd HH:mm:ss 格式
            String timeStamp = dateFormat.format(new Date(issueTime));
            return timeStamp;
        } catch (NumberFormatException e) {

        }
        return createTime;
    }

    public static String date2TimeStamp(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 简单判断是否符合日期格式
     *
     * @param date
     * @return
     */
    public static boolean isDateFormat(String date) {
        if (date == null) {
            return false;
        }
        Pattern regex = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher matcher = regex.matcher(date);
        boolean isMatched = matcher.matches();
        return isMatched;
    }

    /**
     * 字符串转时间Date
     *
     * @param time 时间
     * @return Date
     */
    public static Date timeToDate(String time, SimpleDateFormat sdf) {
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static String dateToTimeStr(Date time, SimpleDateFormat sdf) {
        return sdf.format(time);
    }

    /**
     * 获取多少天之前的数据
     *
     * @param day
     * @return
     */
    public static long getTimeBeforeDate(int day) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, day);
        long timeInMillis = instance.getTimeInMillis();
        return timeInMillis;
    }
}
